package com.zrrd.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zrrd.pojo.Tea;
import com.zrrd.service.TeaService;
import com.zrrd.service.impl.TeaServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author WanJl
 * @version 1.0
 * @title AddTeaServlet
 * @description
 * @create 2024/1/24 14:15
 */
@WebServlet("/tea/add")
public class AddTeaServlet extends HttpServlet {
    TeaService teaService=new TeaServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前端的请求
        req.setCharacterEncoding("UTF-8");
        //req.getParameter()方法是获取不了JSON数据的
        String prams = req.getReader().readLine();
        //把toSON格式的字符串参数转换成Java对象
        Tea tea = JSON.parseObject(prams, Tea.class);
        Integer count = teaService.addTea(tea);
        //新增完成后，返回给前端的响应信息
        resp.getWriter().write(count>0?"success":"error");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
